package com.hgxd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgxd.entity.pojo.OnOrOff;
import com.hgxd.mapper.OnOrOffMapper;
import com.hgxd.service.OnOrOffService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: wtf
 * @Description: 从机
 * @Date: 2022/10/20/11:14
 */
@Service("OnOrOffService")
public class OnOrOffServiceImpl extends ServiceImpl<OnOrOffMapper, OnOrOff> implements OnOrOffService {

    @Resource
    private OnOrOffMapper onOrOffMapper;

    @Override
    public List<OnOrOff> getStatusByCntrNo(String cntrNo) {
        return onOrOffMapper.getStatusByCntrNo(cntrNo);
    }

    @Override
    public Map<String, List<OnOrOff>> getAllStatus() {
        // 1.先获取所有的cntrNo
        List<OnOrOff> slaveList = onOrOffMapper.selectList(null);
        Set<String> cntrNoSet = slaveList
                .stream()
                .map(OnOrOff::getCntrNo)
                .collect(Collectors.toSet());
        System.out.println("cntrNoSet = " + cntrNoSet);
        // 2.循环查询并添加
        HashMap<String, List<OnOrOff>> map = new HashMap<>();
        for (String cntrNo : cntrNoSet) {
            map.put(cntrNo, onOrOffMapper.getStatusByCntrNo(cntrNo));
        }

        return map;
    }
}
